package info.javafans.servlet;

import info.javafans.dao.CategoryDao;
import info.javafans.dao.CommentDao;
import info.javafans.dao.PostDao;
import info.javafans.domain.Category;
import info.javafans.domain.Comment;
import info.javafans.domain.Post;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class PageServlet
 */
public class PageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PageServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String page = request.getParameter("page");

		if (page == null) {

		} else if (page.equals("about")) {
			this.doAbout(request, response);
		}
	}

	//about
	private void doAbout (HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PostDao postDao = new PostDao();
		
		//获得文章分类列表--common
		CategoryDao categoryDao = new CategoryDao();
		List<Category> categoryList = categoryDao.selectAllCategory();
		request.setAttribute("categoryList", categoryList);
		
		//获得viewCount排名文章列表--common
		List<Post> viewRankList = postDao.selectMostViewTitle();
		request.setAttribute("viewRankList", viewRankList);
		
		//获得评论列表--common
		CommentDao commentDao = new CommentDao();
		List<Comment> newCommentList = commentDao.selectNewComments();
		request.setAttribute("newCommentList", newCommentList);
		
		//获得最新文章标题列表--common
		List<Post> newPostTitleList = postDao.selectNewPostTitle();
		request.setAttribute("newPostTitleList", newPostTitleList);
		
		request.getRequestDispatcher("pageAbout.jsp").forward(request, response);
	}

}
